package com.yang.leather.admin.action.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yang.leather.admin.action.BaseAction;
import com.yang.leather.common.upload.UploadException;
import com.yang.leather.common.upload.IUploadService;
/**
 * CKEditor请求处理类
 */
@Scope("prototype")
@Controller("common.ckeditorAct")
public class CKEditor extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	
	protected IUploadService uploadService;

	public String upload(){
		String uploadRuleId = this.getRequest().getParameter("uploadRuleId");
		String uploadType = this.getRequest().getParameter("uploadType");
		String filePath = "";
		String content = "文件上传成功";
		try {
			filePath = uploadService.uploadByType(this.getRequest(), "upload", uploadRuleId,uploadType);
			
			filePath = this.getRequest().getContextPath() + filePath + ".jpg" ;
		} catch (UploadException e) {
			content = e.getMessage();
		}
	    
	    StringBuilder builder = new StringBuilder();
	    builder.append("<script type=\"text/javascript\">")
	    	.append("window.parent.CKEDITOR.tools.callFunction(1,")
	    	.append("\"" + filePath + "\",")
	    	.append("\"" + content + "\")")
			.append(";</script>");
		
	    renderText(builder.toString());
		return null;
	}

	public IUploadService getUploadService() {
		return uploadService;
	}
	
	@Autowired
	public void setUploadService(@Qualifier("uploadService")
			IUploadService uploadService) {
		this.uploadService = uploadService;
	}
	
}
